package com.meida.module.bus.provider.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.module.admin.client.entity.BaseUser;
import com.meida.module.bus.client.enums.UserTypeEnum;
import com.meida.module.pay.client.entity.PayCashout;
import com.meida.module.pay.client.enums.PayCashoutStatusEnum;
import com.meida.module.pay.provider.service.PayCashoutService;
import com.meida.module.user.client.entity.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 后台提现申请列表列表
 */
@Component
public class PayCashOutPageListHandler implements PageInterceptor<PayCashout> {
    @Autowired
    private PayCashoutService payCashoutService;


    @Override
    public void prepare(CriteriaQuery<PayCashout> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        cq.select(PayCashout.class, "cashoutId", "userId", "userType", "shopId", "money", "commission", "ratio", "realMoney", "cashoutType", "aliPayAccount", "wxOpenId", "status", "reason", "createTime");
        cq.select(AppUser.class, "userName");
        cq.select("baseUser.userName operatorUserName");
        cq.like(AppUser.class, "userName");
        cq.eq(PayCashout.class, "status");
        cq.eq(PayCashout.class, "cashoutType");
        cq.createJoin(AppUser.class);
        cq.createJoin(BaseUser.class).setJoinAlias("baseUser").setMainField("operatorId").setJoinField("userId");
        cq.orderByDesc("cashout.createTime");
    }

    @Override
    public void complete(CriteriaQuery<PayCashout> cq, List<EntityMap> result, EntityMap extra) {
        result.forEach(entityMap -> {
            String userType = entityMap.get("userType");
            UserTypeEnum userTypeEnum = UserTypeEnum.getByValue(userType);
            entityMap.put("userTypeText", "");
            if (FlymeUtils.isNotEmpty(userTypeEnum)) {
                entityMap.put("userTypeText", userTypeEnum.getLabel());
            }
        });
        //已处理提现
        Long successNum = payCashoutService.countByCashoutStatus(PayCashoutStatusEnum.success.getValue());
        //待处理提现
        Long dealWithNum = payCashoutService.countByCashoutStatus(PayCashoutStatusEnum.dealWith.getValue());
        //已关闭提现
        Long failNum = payCashoutService.countByCashoutStatus(PayCashoutStatusEnum.fail.getValue());
        extra.put("successNum", successNum);
        extra.put("dealWithNum", dealWithNum);
        extra.put("failNum", failNum);

    }
}
